﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZX.Web.UI.Controls;
using ZX.DAL.DBExtend;

namespace ZX.WebApp.EHR
{
    public partial class hrEmployeeAjax : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string flag = this.GetStringFromQueryString("flag", "");
            switch (flag.Trim())
            {
                case "SavePartTimePost"://保存兼职岗位信息
                    string empId = this.GetStringFromQueryString("empId", string.Empty);//人员ID
                    string deptId=this.GetStringFromQueryString("deptId", string.Empty);//部门ID
                    string postId = this.GetStringFromQueryString("postId", string.Empty);//岗位ID
                    string validDate = this.GetStringFromQueryString("validDate", string.Empty);//生效日期
                    string failDate=this.GetStringFromQueryString("failDate", string.Empty);//失效日期
                    string id=this.GetStringFromQueryString("id", string.Empty);//是否为修改的标志
                    if (empId == string.Empty || postId == string.Empty || validDate == string.Empty || deptId==string.Empty)
                    {
                        HttpContext.Current.Response.Write("-1");
                        return;
                    }
                    try
                    {
                        ZX.EHR.Library.hrEmployeePositions instance = ZX.Common.Globals.getEntity("hrEmployeePositions") as ZX.EHR.Library.hrEmployeePositions;
                        ZX.BLL.Extend.BaseBussnies hrBll = new ZX.BLL.Extend.BaseBussnies(instance);
                        if (id == string.Empty)//新增
                        {
                            instance.ID = Guid.NewGuid();
                            instance.EmployeeID = new Guid(empId);
                            instance.DeptID = new Guid(deptId);
                            instance.PostID = new Guid(postId);
                            instance.ValidDate = DateTime.Parse(validDate);
                            instance.FailDate = DateTime.Parse(failDate);
                            instance.ReportDate = DateTime.Now;
                            instance.AddTime = DateTime.Now;
                            instance.AddUser = this.User.UserID;
                            instance.PostType = 1;//类型为兼职
                            if (instance.ValidDate < DateTime.Now)
                            {
                                instance.AvilableStatus = 2;
                            }
                            else
                            {
                                instance.AvilableStatus = 3;
                            }
                            hrBll.Insert(instance);
                        }
                        else //修改
                        {
                            ZX.EHR.Library.hrEmployeePositions hrPartTimePost = hrBll.GetEntityById(id) as ZX.EHR.Library.hrEmployeePositions;
                            hrPartTimePost.DeptID = new Guid(deptId);
                            hrPartTimePost.PostID = new Guid(postId);
                            hrPartTimePost.ValidDate = DateTime.Parse(validDate);
                            hrPartTimePost.FailDate = DateTime.Parse(failDate);
                            hrBll.Update(hrPartTimePost);
                        }
                        HttpContext.Current.Response.Write("0");
                    }
                    catch (Exception ex)
                    {
                        HttpContext.Current.Response.Write("e_"+ex.Message);
                    }
                    break;
                case "CheckPartTimePost": //检查岗位是否有人全职，或兼职
                    break;
                case "uploadfile"://上传员工照片
                    break;
                case "userTree"://选择用户/员工
                    string empIndeptId = this.GetStringFromQueryString("deptId", "");
                    if (empIndeptId == "") return;
                    DbSql dbsql = new DbSql();
                    System.Data.DataTable dt = dbsql.ExecuteQuery("select * from dbo.FnGetDeptEmployeeByDeptId('" + empIndeptId + "')");
                    string json = ZX.EHR.Web.JSONHelper.GetArrayJSON(dt, "id", "pid");
                    HttpContext.Current.Response.Write(json);
                    break;
            }
        }
    }
}